<template>
  <div class="TVMDanTaiSheBeiCY">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="getList"
        @on-load="getList">
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small" v-if="permissionList.previewEditor && (scope.row.kyReplyStatus==0 || scope.row.assistInvestStatus==2 || scope.row.assistInvestStatus==5)" plain @click.stop="previewEditor(scope.row,1)">编辑</el-button>
          <el-button type="text" size="small" plain @click.stop="previewEditor(scope.row,2)">预览</el-button>
          <el-button type="text" size="small"
                     v-if="(scope.row.assistInvestStatus==3 || scope.row.assistInvestStatus==7) && permissionList.stationMasterAuditApprove"
                     plain @click.stop="stationMasterAuditApproveOption(scope.row)">车站审核通过</el-button>
          <el-button type="text" size="small"
                     v-if="(scope.row.assistInvestStatus==3 || scope.row.assistInvestStatus==7) && permissionList.stationMasterAuditReject"
                     plain @click.stop="stationMasterAuditReject(scope.row)">车站审核驳回</el-button>
        </template>
        <template slot="menuRight">
          <el-button type="primary" v-if="permissionList.previewEditor" size="mini" @click="submit">提交</el-button>
<!--          <el-button type="primary"  size="mini" @click="exportData">导出</el-button>-->
        </template>
        <template slot-scope="scope" slot="tvmCodeSearch">
          <el-select v-model="searchForm.tvmCode" placeholder="请选择" style="width: 150px" filterable clearable>
            <el-option
              v-for="(item,index) in tvmCodes"
              :key="index"
              :label="item.tvmCode"
              :value="item.tvmCode">
            </el-option>
          </el-select>
        </template>
      </avue-crud>
      <el-dialog
        :visible.sync="stationMasterAuditRejectShow"
        title="站长审核意见"
        width="50%"
      >
        <el-form v-if="stationMasterAuditRejectShow">
          <avue-form
            v-model="stationMasterAuditRejectForm"
            :option="stationMasterAuditRejectOption"></avue-form>
        </el-form>
        <div class="bottom-menu-invalid">
          <el-button type="primary" @click="subForm">保存</el-button>
          <el-button type="primary" @click="stationMasterAuditRejectShow = false" >取消</el-button>
        </div>
      </el-dialog>

      <el-dialog
        :visible.sync="stationMasterAuditRejectOptionShow"
        title="站长审核意见"
        width="50%"
      >
        <el-form v-if="stationMasterAuditRejectOptionShow">
          <avue-form
            v-model="stationMasterAuditRejectForm"
            :option="stationMasterAuditRejectOption"></avue-form>
        </el-form>
        <div class="bottom-menu-invalid">
          <el-button type="primary" @click="stationMasterAuditApprove">保存</el-button>
          <el-button type="primary" @click="stationMasterAuditRejectOptionShow = false" >取消</el-button>
        </div>
      </el-dialog>
    </basic-container>
    <HuikuiInfoKeYun ref="HuikuiInfoKeYun" v-if="isShowDia"></HuikuiInfoKeYun>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import HuikuiInfoKeYun from './HuikuiInfoKeYun';
  import {exportDataToExcel} from "../../../../../../api/subwayRat/exportSelf";
  import { handleExcelResponse } from "@/util/util";
  import {
    setSearchFiledsEmpty,
    getSwPmDeviceOperationCodeList,
    getSysUserToLS,
    getSysDictItemListToDict,
    getStationByCode,getLineAndStationByCode,
    getSwPmTvmCodeList, getLineAllAsyn,getLineCodes
  } from "../../../../../../api/subwayRat/commonCheck";
  import {tableOption,getSwAuTvmSingleAssistListKY,updateSwAuTvmSingleAssistKYToStatus,exportTvmSingle,auditTVMDanTaiSheBei,stationMasterAuditRejectOption
  } from '../../../../../../api/stationTiecketManager/shuJuXieChaGuanLi/TVM/XT/DTSB/TVMDanTaiSheBeiCYKeYun';
  import store from '@/store'
  import {
    auditSwAuStDiffNotice
  } from "../../../../../../api/stationTiecketManager/xianWngCJXXQR/cheZhanPKCY/cheZhanPKCYQRTZ/zhanNeiRYDLCK";
  import fa from "element-ui/src/locale/lang/fa";
  export default {
    name: 'TVMDanTaiSheBeiCYKeYun',
    components:{
      HuikuiInfoKeYun
    },
    data() {
      return {
        stationMasterAuditRejectOption: stationMasterAuditRejectOption,
        stationMasterAuditRejectForm: {},
        stationMasterAuditRejectShow: false,
        stationMasterAuditRejectOptionShow: false,
        isShowDia:false,
        option: tableOption,
        optionForm: {},
        formData: {},
        listLoading: true,
        selectKeys:[], //存放对像
        selectKeyVals:[], //存放值
        lineCodes:[],
        stationCodes:[],
        tvmCodes:[],
        searchForm:{
          lineCode:'',
          stationCode:'',
          tvmCode:''
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        ids : [],
        list : [],
        showType : 0,
      }
    },
    computed: {
      ...mapGetters(['permissions']),
      permissionList () {
        return {
          stationMasterAuditApprove: this.vaildData(
            this.permissions.TVMDanTaiSheBeiCYKeYun_AUDIT_STATION_PASS,
            false
          ),
          stationMasterAuditReject: this.vaildData(
            this.permissions.TVMDanTaiSheBeiCYKeYun_AUDIT_STATION_REJECT,
            false
          ),
          previewEditor: this.vaildData(
            this.permissions.passenger_transport_edit,
            false
          ),
        };
      },
    },
    watch: {
    },
    mounted() {
      // getLineAllAsyn().then(res=>{ //获取线路
      //   this.lineCodes=res.data;
      // });
      getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
      });
      this.getSwPmTvmCodeLists();
    },
    created() {
    },
    methods: {
      setSearchFiledsEmpty,
      async submit(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(!this.selectKeys[i].kyReplyReason || '' == this.selectKeys[i].kyReplyReason){
            this.$message.warning("勾选数据中存在未选择回馈原因数据！");
            return
          }
        }
        let ids=[];
        for(let i=0;i<this.selectKeys.length;i++){
          ids.push(this.selectKeys[i].id);
        }
        let messageLSList = [];
        for(let i=0;i<this.selectKeys.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.selectKeys[i].lineCode,
            "stationCode":this.selectKeys[i].stationCode
          })
        }
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          //title:this.$store.getters.tag.label,
	  title:'TVM单台设备协查车站回馈',
          msgType:"提交",
          type:"TVMDanTaiSheBeiCY_ky",
          ids:ids,
          roleCode: 3
        };
        await updateSwAuTvmSingleAssistKYToStatus(p).then((response)=>{
        });
        this.$message.success("提交成功");
        this.getList();
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      // async exportData(){ // 导出
      //   this.searchForm.pageNo=1;
      //   this.searchForm.pageSize=1000000000;
      //   if(this.selectKeys.length>0){
      //     let newData = JSON.parse(JSON.stringify(this.selectKeys));
      //     exportDataToExcel(newData,this.option.column,'TVM单台设备差异协查');
      //   }else {
      //     let res=await getSwAuTvmSingleAssistListKY(this.searchForm);
      //     let data = res.data.data;
      //     exportDataToExcel(data,this.option.column,'TVM单台设备差异协查');
      //   }
      // },
      async exportData(){ //导出
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            const res = await exportTvmSingle({ids:this.selectKeyVals.join(","), tag:"keyun"});
            handleExcelResponse(res);
          }else{
            this.searchForm.kyAssistInvestStatus = "1";//
            this.searchForm.tag = "keyun";//导出客运标识
            const res = await exportTvmSingle(this.searchForm);
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
      },
      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.HuikuiInfoKeYun.recallShow(row);
        })
      },
      stationMasterAuditApprove(row){
        let th=this;
        if (this.stationMasterAuditRejectForm.auditStationOpinion == null || this.stationMasterAuditRejectForm.auditStationOpinion == '') {
          th.$message.error("请填写审核意见！");
          return false;
        }
        auditTVMDanTaiSheBei({
          id:this.stationMasterAuditRejectForm.id,
          assistInvestStatus:4,
          auditStationOpinion:this.stationMasterAuditRejectForm.auditStationOpinion
        }).then(res=>{
          th.$message.success("站长审核成功！");
          this.stationMasterAuditRejectOptionShow=false;
          th.getList();
        })
          .catch(() => {
            // th.$message.error("站长审核失败！");
            return false;
          });
      },

      stationMasterAuditReject(row){
        this.stationMasterAuditRejectShow=true;
        this.stationMasterAuditRejectForm = row;
      },

      stationMasterAuditApproveOption(row){
        this.stationMasterAuditRejectOptionShow=true;
        this.stationMasterAuditRejectForm = row;
      },

      subForm(){
        let th=this;
        auditTVMDanTaiSheBei({
          id:this.stationMasterAuditRejectForm.id,
          assistInvestStatus:5,
          auditStationOpinion:this.stationMasterAuditRejectForm.auditStationOpinion
        }).then(res=>{
          th.$message.success("车站审核成功！");
          this.stationMasterAuditRejectShow=false;
          th.getList();
        })
          .catch(() => {
            // th.$message.error("车站审核失败！");
            return false;
          });
      },

      getList() {
        this.listLoading = true;
        Object.assign(this.searchForm,this.page);
	this.searchForm.assistCategoryRole='station';//查询下发协查类型
        //this.searchForm.kyAssistInvestStatus = "1";  //20250812
        getSwAuTvmSingleAssistListKY(this.searchForm).then((response)=>{
          this.list = response.data.data;
          this.listLoading = false;
          this.page.total=response.data.total;
        });
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList();
        done()
      },
      changeLine(v){ //改变线路时
        getStationByCode({line_code:v}).then(res=>{
          this.stationCodes=res.data;
          this.searchForm.stationCode='';
          this.searchForm.tvmCode = '';
        });
      },
      changeStation(v){
        getSwPmTvmCodeList({stationCode:v}).then((res)=>{
          this.tvmCodes=res.data;
        });
      },
      getSwPmTvmCodeLists(){
        let lineCodes = store.getters.userInfo.lineCode;
        let stationCodes = store.getters.userInfo.stationCode;
        getSwPmTvmCodeList({lineCode: lineCodes,stationCode:stationCodes}).then((res)=>{
          this.tvmCodes=res.data;
        });
      }
    }
  }
</script>
